package com.bot.adms.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bot.adms.model.vo.CheckRecordInfoVo;
import com.bot.adms.service.impl.ApplyServiceImpl;
import com.bot.adms.service.impl.CheckRecordServiceImpl;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 查看审核记录控制器
 * @author: 刘威林
 */
@WebServlet("/getCheckRecord")
public class CheckRecordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //修改字符集
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //响应体创建
        JSONObject jsonObject = new JSONObject();
        //success定义【true成功，false失败】
        boolean success = false;
        //响应码定义【响应码,200-成功,500-失败】
        int code = 500;
        String msg = "查询失败";
        //data定义【用来返回查看的具体数据】
        JSONArray data = new JSONArray();

        try {
            //1.获取要查看的审核记录id
            String idStr;
            //2.参数验证
            //判断是否传入id数据
            if(req.getParameter("id") != null && !req.getParameter("id").isEmpty()){
                idStr = req.getParameter("id");
            }else{
                code = 500;
                msg = "id必须存在";
                return;
            }

            //判断是否为数字
            Long id;
            try{
                id = Long.parseLong(idStr);
            }catch (NumberFormatException e){
                code = 500;
                msg = "id必须为数值类型";
                return;
            }

            //4.调用业务逻辑
            CheckRecordServiceImpl checkRecordService = new CheckRecordServiceImpl();
            List<CheckRecordInfoVo> checkRecordInfoVoList = checkRecordService.lookCheckRecord(id);
            //5.返回结果
            if (checkRecordInfoVoList != null && !checkRecordInfoVoList.isEmpty()){
                success = true;
                code = 200;
                msg = "查询成功";
                data = (JSONArray) JSON.toJSON(checkRecordInfoVoList);
            } else{
                success = false;
                code = 500;
                msg = "未找到该投放申请id对应的审核数据";
            }
        } catch (Exception e) {
            code = 500;
            msg = "服务器异常，请稍后再试";
            e.printStackTrace();

        } finally {
            //组装JSON
            jsonObject.put("success", success);
            jsonObject.put("code", code);
            jsonObject.put("msg", msg);
            if (success){
                jsonObject.put("data", data);
            }

            //将json转成字符串
            String json = JSON.toJSONString(jsonObject);
            //将json转换为字符串并返回
            ServletOutputStream out = resp.getOutputStream();
            out.write(json.getBytes("UTF-8"));
            out.flush();
            out.close();
        }



    }
}
